﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Mang24k99.Model.Dissertation;
using Mang24k99.Business.Dissertation;
using System.Collections.Generic;
using Castle.ActiveRecord;
using System.IO;


namespace Mang24k99.Web.Dissertation.Reference
{
    public partial class List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["PageIndexNow"] = "1";
            if (!IsPostBack)
            {
                try
                {
                    ViewState["editWeb"] = Request["WEBID"].ToString();
                    selectWeb.SelectedValue = ViewState["editWeb"].ToString();
                    ViewState["PageIndexNow"] = int.Parse(Request["PageIndex"].ToString());
                }
                catch { ViewState["PageIndexNow"] = "1"; }

                int pageNow = int.Parse(ViewState["PageIndexNow"].ToString());
                if (pageNow != 1)
                    BindDissertation(NewsPager.PageSize * (pageNow - 1), NewsPager.PageSize * pageNow);
                else
                    BindDissertation(0, NewsPager.PageSize);
            }
        }

        protected int index = 0;

        private void BindDissertation(int iStart,int iEnd)
        {
            string webID = selectWeb.SelectedValue;
            IList<Dissert> diss=new List<Dissert>();
            int iCount = 0;
            diss = BDissertations.FindDissertations(int.Parse(webID),iStart,iEnd,out iCount,false);
            NewsPager.RecordCount = iCount;
            if (ViewState["PageIndexNow"].ToString() != "1")
            {
                NewsPager.CurrentPageIndex = int.Parse(ViewState["PageIndexNow"].ToString());
                ViewState["PageIndexNow"] = 1;
            }
            dissertationName.DataSource = diss;
            dissertationName.DataBind();
        }

        protected void selectWeb_SelectedIndexChanged(object sender, EventArgs e)
        {
            SpecialWeb.Text = selectWeb.SelectedItem.Text;
            BindDissertation(0, NewsPager.PageSize);
        }

        protected void dissertationName_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                using (new SessionScope())
                {
                    Repeater rep = (Repeater)e.Item.FindControl("referenceTitle");
                    Dissert drv = (Dissert)e.Item.DataItem;
                    string colID = drv.ID;
                    MDissertations mDiss = MDissertations.Find(int.Parse(colID));
                    IList<MDissertationToFile> mdisFile = mDiss.DissertationToFile;
                    if (mdisFile.Count < 1)
                    {
                        HyperLink hl = e.Item.FindControl("updateReference") as HyperLink;
                        hl.Enabled = false;
                        Label ll = e.Item.FindControl("showInfo") as Label;
                        ll.Text = "该专题暂无参考文件!";
                        ll.Visible = true;
                    }
                    rep.DataSource = mdisFile;
                    rep.DataBind();
                }
            }
        }

        protected void referenceTitle_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                string dissName = ((MDissertationToFile)(e.Item.DataItem)).FileTitle;
                ((LinkButton)e.Item.Controls[1]).Attributes.Add("onclick", "javascript:return confirm('您确认要删除参考文件:\""+dissName+"\"吗?');");
            }
        }

        protected void referenceTitle_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName=="DisFile")
            {
                string disFileID = e.CommandArgument.ToString();
                //MDissertations mDis = MDissertations.Find(int.Parse(disFileID));
                MDissertationToFile iMdisFile = MDissertationToFile.Find(long.Parse(disFileID));
                string file = iMdisFile.FilePath;
                if (File.Exists(Request.PhysicalApplicationPath + file))
                    File.Delete(Request.PhysicalApplicationPath + file);//删除参考文件
                BDissertationToFile.Delete(iMdisFile);//删除记录
                BindDissertation(NewsPager.StartRecordIndex - 1, NewsPager.EndRecordIndex);
                
            }
        }

        /// <summary>
        /// 翻页绑定数据
        /// </summary>
        protected void NewsPager_PageChanged(object sender, EventArgs e)
        {
            BindDissertation(NewsPager.StartRecordIndex - 1, NewsPager.EndRecordIndex);
        }

        protected void dissList_Click(object sender, EventArgs e)
        {
            Response.Redirect("../List.aspx");
        }

        protected void addDisFile_Click(object sender, EventArgs e)
        {
            Response.Redirect("Edit.aspx");
        }
    }
}
